package com.ezjie.ielts.module_read.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.constant.Constants;
import com.ezjie.ielts.module_read.PhotoViewActivity;
import com.ezjie.ielts.util.DensityUtil;
import com.lidroid.xutils.BitmapUtils;

public class ReadOriginpagerAdapter extends BaseAdapter {

	private Context mContext;
	private List<String> lists = new ArrayList<String>();
	private boolean hasImage;
	private BitmapUtils bUtils;

	public ReadOriginpagerAdapter(Context context, List<String> lists) {
		mContext = context;
		bUtils = new BitmapUtils(mContext);
		bUtils.configDefaultLoadingImage(R.drawable.default_img);// 默认背景图片
		bUtils.configDefaultLoadFailedImage(R.drawable.default_img);// 加载失败图片
		if (lists != null) {
			this.lists = lists;
		}
	}

	public void addDatas(List<String> lists, boolean hasImage) {
		this.lists = lists;
		this.hasImage = hasImage;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_originpager_layout, null);
			vh = new ViewHolder();
			vh.txt = (TextView) convertView.findViewById(R.id.tv_origin_content);
//			Typeface face = Typeface.createFromAsset (mContext.getAssets() , "fonts/palatino.ttf");
//			vh.txt.setTypeface (face);
			vh.img = (ImageView) convertView.findViewById(R.id.iv_origin_content);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		int padding = DensityUtil.dip2px(mContext, 15);
		if (position == lists.size() - 1 && hasImage) {
			vh.img.setVisibility(View.VISIBLE);
			vh.img.setPadding(padding, padding, padding, padding * 3);
			vh.img.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, PhotoViewActivity.class);
					intent.putExtra(Constants.READ_IMG_URL, lists.get(position));
					mContext.startActivity(intent);					
				}
			});
			vh.txt.setVisibility(View.GONE);
			bUtils.display(vh.img, lists.get(position));
		} else {
			vh.img.setVisibility(View.GONE);
			vh.txt.setVisibility(View.VISIBLE);
			if (position == 0) {
				vh.txt.setBackgroundResource(R.drawable.originpager_notop);
				vh.txt.setPadding(padding, 0, padding, padding);
			} else if (position == lists.size() - 1) {
				vh.txt.setBackgroundResource(R.drawable.originpager_nomal);
				vh.txt.setPadding(padding, padding, padding, padding * 3);
			} else {
				vh.txt.setBackgroundResource(R.drawable.originpager_nomal);
				vh.txt.setPadding(padding, padding, padding, padding);
			}
			vh.txt.setText(Html.fromHtml(lists.get(position)));
		}
		return convertView;
	}

	public class ViewHolder {
		public TextView txt;
		public ImageView img;
	}

}
